package Controller;

import entity.Cake;
import entity.CakeData;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

public class CakeListServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("<html><head><title>蛋糕商店</title></head><body>");
        out.println("<h1>欢迎选购蛋糕</h1>");
        out.println("<div style='display: grid; gap: 20px;'>");

        for (Cake cake : CakeData.getAllCakes()) {
            out.println("<div style='border: 1px solid #ccc; padding: 10px;'>");
            out.println("<form method='post' action='AddToCartServlet'>");
            out.println("<h3>" + cake.getName() + "</h3>");
            out.println("<p>价格：￥" + cake.getPrice() + "</p>");
            out.println("<input type='hidden' name='id' value='" + cake.getId() + "'>");
            out.println("<button type='submit'>加入购物车</button>");
            out.println("</form></div>");
        }

        out.println("</div>");
        out.println("<div style='margin-top: 20px;'>");
        out.println("<a href='ViewCartServlet'>查看购物车</a>");
        out.println("</div></body></html>");
    }
}
